package com.aysafety.iotdataplatformweb.controller.hydraulicsupport;

import com.aysafety.core.common.CheckUtils;
import com.aysafety.core.common.DataConversionUtil;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.hydraulicsupport.HydraulicSupportService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mineMonitor/realMonitor/hydraulicSupport")
public class HydraulicSupportController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    HydraulicSupportService hydraulicSupportService;

    @RequestMapping("/hydraulicSupportRealTime")
    public Map<String, Object> hydraulicSupportRealTime(@RequestParam Map<String, Object> params,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId") || !params.containsKey("equipmentId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String equipmentId = params.get("equipmentId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "0";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "0";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "s";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "s";
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            /*String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            int size = Integer.parseInt(sizeStr);
            int size = 10000;*/
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            data.put("total", hydraulicSupportService.countByHydraulicSupport(mineId, equipmentId, startTime, endTime,
                timeSpan, timeUnit));
            List<Map<String, Object>> result = hydraulicSupportService.queryRealTimeByHydraulicSupport(mineId, equipmentId,
                startTime, endTime, timeSpan, timeUnit, page, 0, startRow);
            data.put("list", result);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("HydraulicSupportController-hydraulicSupportRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("HydraulicSupportController-hydraulicSupportRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    @RequestMapping("/frequency")
    public Map<String, Object> frequency(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId") || !params.containsKey("equipmentId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String equipmentId = params.get("equipmentId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "30";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "30";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "m";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "m";
            List<Map<String, Object>> list =
                hydraulicSupportService.queryFrequency(mineId, equipmentId, startTime, endTime, timeSpan, timeUnit);
            data.put("list", list);
            data.put("firstStartRow", null);
            data.put("lastStartRow", null);
            data.put("total", list.size());
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("HydraulicSupportController-frequencyHistory:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("HydraulicSupportController-frequencyHistory:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    @RequestMapping("/flicker")
    public Map<String, Object> flicker(@RequestParam Map<String, Object> params, HttpServletRequest request,
                                       HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId") || !params.containsKey("equipmentId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String equipmentId = params.get("equipmentId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                    : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                    StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                    : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "30";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "30";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "m";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "m";

            List<Map<String, Object>> list =
                    hydraulicSupportService.queryFlicker(mineId, equipmentId, startTime, endTime, timeSpan, timeUnit);
            logger.info("返回数据： " + list);
            System.out.println(list);
            //data.put("deviceId", equipmentId);
            data.put("list", list);
            data.put("firstStartRow", null);
            data.put("lastStartRow", null);
            data.put("total", list.size());
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("HydraulicSupportController-flickerHistory:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("HydraulicSupportController-flickerHistory:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }



    /*public Map<String, Object> flicker(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId") || !params.containsKey("equipmentId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String equipmentId = params.get("equipmentId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "30";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "30";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "m";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "m";
            List<Map<String, Object>> list =
                hydraulicSupportService.queryFlicker(mineId, equipmentId, startTime, endTime, timeSpan, timeUnit);
            data.put("list", list);
            data.put("firstStartRow", null);
            data.put("lastStartRow", null);
            data.put("total", list.size());
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("HydraulicSupportController-flickerHistory:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("HydraulicSupportController-flickerHistory:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }*/

    /**
     * @Description: 综合预警
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String , java.lang.Object>
     * @Author: ChengQi
     * @Date: 2019/1/3
     */
    @RequestMapping("/comprehensiveWarning") public Map<String, Object> comprehensiveWarning(
        @RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();

            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);

            // 设备id
            //String deviceId = DataConversionUtil.getStrFromMap("equipmentId", params);

            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);

            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = hydraulicSupportService.comprehensiveWarning(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

}
